import random
import os
import re
import time

admin=['天一','大空翼','暖哥','jski匪姐','柚子','大菠','赵嘻嘻','陈6岁🏂']
keys=['【装备】','【尺码】','【价格】','【新旧】','【联系】']

def clearmsg():
    f = open('f2hand.txt','w+')
    f.write('')
    f.close()

def addmsg(s,name,qunnum):
    file = open("f2hand.txt",'r')
    lines = file.readlines()
    file.close()
    name=name+' (约车%s群)'%(qunnum)
    f = open('f2hand.txt','a')
    if lines==[]:
        f.write(str(1)+':'+s+'@%s '%(name)+'\n')
    else:
        num=int((lines[-1])[0:1])
        f.write(str(num+1)+':'+s+'@%s '%(name)+'\n')
    f.close()

def showmsg():
    f=open('f2hand.txt', 'r')
    lines = f.readlines()
    strshow = '📣二手装备交易助手\n发布方法(j2卖+正确格式内容)\n删除方法(j2删+序号)\n查看方法(直接发个j2)\n查看格式(直接发个j2卖)'
    for line in lines:
        line = line.replace('【','\n【')
        strshow = strshow+'\n'+line
    f.close()
    return strshow

def delnum(delnum,name):
    file = open("f2hand.txt",'r')
    lines = file.readlines()
    n = 1
    with open("f2hand.txt",'w') as f:
        for line in lines:
            num = line[0:1]
            if int(n) == int(delnum):
                if name+' ' in line or name in admin:
                    msg = '二手信息 %s 已删除'%(delnum)
                    delnum = False
                else:
                    msg = '只有发布人和管理员可以删除'
                    newline=str(n)+line[1:]
                    f.write(newline)
                    n=n+1
            else:
                newline = str(n)+line[1:]
                f.write(newline) 
                n=n+1
        f.close()
        return msg

def tohandhelp(act,name,qunnum):
    act = act.replace('\n', '')
    act = act.replace(' ','')
    act = act.replace('***复制本条发布二手信息***','')
    name=name.replace(' ','')
    qunnum = int(qunnum)
    for key in keys:
        flag = key in act
        flag = flag and flag
    if act=='j2':
        msgshow=showmsg()
        return msgshow  
    elif 'j2删' in act:
        act = act.replace('j2删','')
        act=int(act)
        return delnum(act,name)
    elif 'j2卖' in act:
        if flag:
            act = act.replace('j2卖','') 
            addmsg(act,name,qunnum)
            msgshow = showmsg()
            return msgshow
        else:
            msg='j2卖\n【装备】\n【尺码】\n【新旧】\n【价格】\n【联系】\n***复制本条发布二手信息***'
            return msg

